/**
 * 
 */
package base.img;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**  
 * @Project: Test
 * @Title: ImageUtil.java
 * @Package test
 * @Description: TODO
 * @author liuboen liuboen@zba.com
 * @date 2011-9-21 ����06:20:02
 * @version V1.0  
 */

public class JDKImageProcessor {
	private static String FILE_SUFFIX = ".";
	private static String WATERMARKS = "WM";
	private static String SMALL_SIZE = "140140";

	private final static String getRealTargetFile(File oriFile,
			String modifyName) {
		String filePath = oriFile.getParent();
		String fileName = oriFile.getName();
		int suffixPos = fileName.lastIndexOf(FILE_SUFFIX);
		String realFileName = fileName.substring(0, suffixPos) + "_"
				+ modifyName;
		String suffixName = fileName.substring(suffixPos + 1);
		StringBuffer sb = new StringBuffer();
		sb.append(filePath).append(File.separator).append(realFileName).append(
				FILE_SUFFIX).append(suffixName);
		return sb.toString();
	}
	/**
	 * ����
	 * 
	 * @param filePath
	 *            ͼƬ·��
	 * @param height
	 *            �߶�
	 * @param width
	 *            ���
	 * @param bb
	 *            �����ʱ�Ƿ���Ҫ����
	 * @throws IOException
	 */
	public final static String resize(String filePath, int height, int width,
			boolean bb) throws IOException {
		double ratio = 0.0; // ���ű���
		File f = new File(filePath);
		BufferedImage bi = ImageIO.read(f);
		Image itemp = bi.getScaledInstance(width, height,
				BufferedImage.SCALE_SMOOTH);
		// �������
		if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
			if (bi.getHeight() > bi.getWidth()) {
				ratio = (new Integer(height)).doubleValue() / bi.getHeight();
			} else {
				ratio = (new Integer(width)).doubleValue() / bi.getWidth();
			}
			AffineTransformOp op = new AffineTransformOp(AffineTransform
					.getScaleInstance(ratio, ratio), null);
			itemp = op.filter(bi, null);
		}
		if (bb) {
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, width, height);
			if (width == itemp.getWidth(null))
				g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
						itemp.getWidth(null), itemp.getHeight(null),
						Color.white, null);
			else
				g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp
						.getWidth(null), itemp.getHeight(null), Color.white,
						null);
			g.dispose();
			itemp = image;
		}
		String realFile = getRealTargetFile(f, width+"x"+height);
		ImageIO.write((BufferedImage) itemp, "jpg", new File(realFile));
		return realFile;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			resize("C:\\Users\\Administrator\\Desktop\\tempphoto\\min01.jpg", 225, 550,true);
			resize("C:\\Users\\Administrator\\Desktop\\tempphoto\\min01.jpg", 45, 75,true);
		} catch (IOException e) {
			
		}

	}

}
